package com.easylinkin.linkappapi.common.iaqi;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.LinkedList;
import java.util.List;

/**
 * 空气质量分指数
 *
 * @author tongjie
 */
public class IaqiUtil {
    static List<IaqiVo> iqaiList;

    static {
        iqaiList = new LinkedList<>();
        iqaiList.add(new IaqiVo(0, 0, 0));
        iqaiList.add(new IaqiVo(50, 50, 35));
        iqaiList.add(new IaqiVo(100, 150, 75));
        iqaiList.add(new IaqiVo(150, 250, 115));
        iqaiList.add(new IaqiVo(200, 350, 150));
        iqaiList.add(new IaqiVo(300, 420, 250));
        iqaiList.add(new IaqiVo(400, 500, 350));
        iqaiList.add(new IaqiVo(500, 600, 500));
    }

    /**
     * @param pm10
     * @param pm25
     * @return 获取空气质量等级
     */
    public static int getIaqiLevel(int pm10, int pm25) {
        int val = getIaqiValueByPm(pm10, pm25);
        if (val <= 50) {
            return 1;
        } else if (val >= 51 && val <= 100) {
            return 2;
        } else if (val >= 101 && val <= 150) {
            return 3;
        } else if (val >= 151 && val <= 200) {
            return 4;
        } else if (val >= 201 && val <= 300) {
            return 5;
        }
        return 6;

    }

    /**
     * @param pm10 pm10
     * @param pm25 pm2.5
     * @return IAQI 空气分指数
     */
    public static int getIaqiValueByPm(int pm10, int pm25) {
        int val10 = getIaqiValueByPmType(pm10, 10);
        int val25 = getIaqiValueByPmType(pm25, 25);
        return val25 > val10 ? val25 : val10;
    }

    /**
     * @param value pm 值
     * @param type  计算类型 10-pm10   25-pm25
     * @return
     */
    static int getIaqiValueByPmType(int value, int type) {
        int index = getIndex(value, type);
        if (index > (iqaiList.size() - 1)) {
            return iqaiList.get(iqaiList.size() - 1).getIaqiValue();
        } else if (index == 0) {
            return iqaiList.get(0).getIaqiValue();
        }
        IaqiVo low = iqaiList.get(index - 1);
        IaqiVo hight = iqaiList.get(index);
        int hightPm = 0;
        int lowPm = 0;
        if (type == 10) {
            hightPm = hight.getPm10();
            lowPm = low.getPm10();
        } else if (type == 25) {
            hightPm = hight.getPm25();
            lowPm = low.getPm25();
        }

        Double v = 1.0 * (hight.getIaqiValue() - low.getIaqiValue()) / (hightPm - lowPm);
        v = v * (value - lowPm) + low.getIaqiValue();
        return v.intValue();
    }

    private static int getIndex(int value, int type) {
        int i = 0;
        for (; i < iqaiList.size(); i++) {
            int val;
            if (type == 10) {
                val = iqaiList.get(i).getPm10();
            } else {
                val = iqaiList.get(i).getPm25();
            }
            if (value < val) {
                return i;
            }
        }

        return i;
    }



    @Data
    @AllArgsConstructor
    private static class IaqiVo {
        /**
         * 空气质量分指数
         */
        private int iaqiValue;
        /**
         * PM 10 值
         */
        private int pm10;
        /**
         * PM 2.5 值
         */
        private int pm25;

    }

}
